package com.howells.time.base.activity;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.howells.time.R;


/**
 * Created by Administrator on 2016.4.7.
 */
public class CustomDialog extends Dialog implements View.OnClickListener{

    private Context context;
    private String title;
    private String confirmButtonText;
    private String cacelButtonText;
    private String message;
    private Dialoglistener dialoglistener;

    public void setDialoglistener(Dialoglistener dialoglistener) {
        this.dialoglistener = dialoglistener;
    }
    @Override
    public void onClick(View view) {
        dialoglistener.onclick(view);
    }

    public interface Dialoglistener{
        public void onclick(View view);
    }
    public CustomDialog(Context context, String title, String message, String confirmButtonText, String cacelButtonText) {
        super(context);
        this.context = context;
        this.title = title;
        this.message=message;
        this.confirmButtonText = confirmButtonText;
        this.cacelButtonText = cacelButtonText;
    }

    public CustomDialog(Context context, int theme) {
        super(context, theme);
    }

    protected CustomDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    private void init() {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.view_common_dialog_layout_ex, null);
        setContentView(view);

        TextView tvTitle = (TextView) view.findViewById(R.id.view_common_dialog_top_title);
        TextView tvmessage= (TextView) view.findViewById(R.id.view_common_dialog_content);
        TextView tvConfirm = (TextView) view.findViewById(R.id.view_common_dialog_left_button);
        TextView tvCancel = (TextView) view.findViewById(R.id.view_common_dialog_right_button);
        tvTitle.setText(title);
        tvmessage.setText(message);
        tvConfirm.setText(confirmButtonText);
        tvCancel.setText(cacelButtonText);
        tvConfirm.setOnClickListener(this);
        tvCancel.setOnClickListener(this);

        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.width = (int) (d.widthPixels * 0.8); // 高度设置为屏幕的0.6
        dialogWindow.setAttributes(lp);
    }

}
